package com.ymatou.iapi.shoppingcart.testcase;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import com.ymatou.iapi.shoppingcart.parameter.DeleteShoppingCartBean;
import com.ymatou.iapi.shoppingcart.service.DeleteShoppingCartCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppShoppingCartService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_DeleteShoppingCart {
	
	private static DeleteShoppingCartBean deleteShoppingCartBean;
	private static DeleteShoppingCartCall deleteShoppingCartCall;
	
	private static int buyerUserId = 20226821;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除购物车");
	}

	@Before
	public void caseUp() throws Exception {
		deleteShoppingCartBean = new DeleteShoppingCartBean();
		deleteShoppingCartCall = new DeleteShoppingCartCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteShoppingCartCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_DeleteShoppingCart_001() {
		 Logger.start(true,"验证删除购物车商品是否成功");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, buyerUserId, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + buyerUserId + "}").get("_id").toString();
			 
			 ArrayList<String> scIds = new ArrayList<String>();
			 scIds.add(scId);

			 deleteShoppingCartBean.setScIds(scIds);
			 deleteShoppingCartBean.setUserId(buyerUserId);
			 
			 deleteShoppingCartCall.setData(deleteShoppingCartBean);
			 deleteShoppingCartCall.callService();
			 
			 Logger.comment("deleteShoppingCartCall send:" + deleteShoppingCartCall.getEntityString());
			 Logger.comment("deleteShoppingCartCall response:" + deleteShoppingCartCall.getReturnData());
			 
			 boolean isExist = false;
			 
			 if (wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + buyerUserId + "}") == null)
				 isExist = true;
			 
			 Logger.verifyEquals(isExist, deleteShoppingCartCall.getResult().getBoolean("success"), "验证删除购物车商品是否成功");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_DeleteShoppingCart_002() {
		 Logger.start(true,"验证批量删除购物车商品是否成功");
		 try {
			 String productId1 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId1, buyerUserId, 1);
			 service.addShoppingCart(productId2, buyerUserId, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId1 = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId1 + "', "
			 		+ "'userid':" + buyerUserId + "}").get("_id").toString();
			 String scId2 = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId2 + "', "
				 		+ "'userid':" + buyerUserId + "}").get("_id").toString();
			 
			 ArrayList<String> scIds = new ArrayList<String>();
			 scIds.add(scId1);
			 scIds.add(scId2);

			 deleteShoppingCartBean.setScIds(scIds);
			 deleteShoppingCartBean.setUserId(buyerUserId);
			 
			 deleteShoppingCartCall.setData(deleteShoppingCartBean);
			 deleteShoppingCartCall.callService();
			 
			 Logger.comment("deleteShoppingCartCall send:" + deleteShoppingCartCall.getEntityString());
			 Logger.comment("deleteShoppingCartCall response:" + deleteShoppingCartCall.getReturnData());
			 
			 boolean isExist = false;
			 
			 if (wapper.getOneShoppingCartInfoForMap("{'productid':{$in:['" + productId1 + "', '" + productId2 + "'"
			 		+ "]}, 'userid':" + buyerUserId + "}") == null)
				 isExist = true;
			 
			 Logger.verifyEquals(isExist, deleteShoppingCartCall.getResult().getBoolean("success"), "验证批量删除购物车商品是否成功");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
}
